// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: "cloud1-5g9np9s88a72a0bc"
});

// 云函数入口函数
exports.main = async (event, context) => {
  console.log("ID", event.openid)
  console.log("IDIID", event.openid)
  let name = 'userInfo.name'
  let idNum = 'userInfo.idNum'
  let ID = 'userInfo.ID'
  let phone = 'userInfo.phone'
  let regionDetail='userInfo.regionDetail'
  let regionDetailObject='userInfo.regionDetailObject'
  let region = 'userInfo.region'
  let avaUrl = 'userInfo.avaUrl'
  if (event.action == "userInfo") {
    let res = await cloud.database().collection('all_user')
      .where({
        _openid: event.openid
      })
      .update({
        data: {
          [name]: event.Name,
          [idNum]: event.code,
          [ID]: event.ID,
          [phone]: event.phone,
          [region]: event.region,
          [regionDetail]:event.regionDetail,
          [avaUrl]: event.avaUrl,
          [regionDetailObject]:event.regionDetailObject
        }
      })
      .then(res => {
        console.log("添加成功", res)
      })
    return res
  }
  if (event.action == "prize") {
    return await
    cloud.database().collection('all_user')
      .where({
        _openid: event.openid
      })
      .update({
        data: {
          prize: cloud.database().command.push({
            time: event.time,
            prizeName: event.prizeName,
          }),
          address: event.address
        }
      })
      .then(res => {
        console.log("添加成功", res)
      })
  }
  if (event.action == "act_admit") {
    console.log(event.openid)
    let res = await
    cloud.database().collection('all_user')
      .where({
        _id: event.openid
      })
      .update({
        data: {
          has_be_admit: cloud.database().command.push({
            project_id: event.project_id,
            projectName: event.projectName,
          }),
          will_be_admit:cloud.database().command.pull({
            project_id: event.project_id
          })
        }
      })
    return res
  }
  if (event.action == "act_no_admit") {
    console.log(event.openid)
    let res = await
    cloud.database().collection('all_user')
      .where({
        _id: event.openid
      })
      .update({
        data: {
          has_be_reject: cloud.database().command.push({
            project_id: event.project_id,
            projectName: event.projectName,
          }),
          will_be_admit:cloud.database().command.pull({
            project_id: event.project_id
          })
        }
      })
    return res
  }
  if(event.action=="point")
  {
    let point=await
    cloud.database().collection('all_user')
      .where({
        _openid: event.openid
      })
      .update({
        data: {
          point: cloud.database().command.push({
            time: event.time,
            reason: event.reason,
            pointDetail:event.pointDetail
          }),
        }
      })
      return point
  }
  if(event.action=="exe")
  {
    let exe= await
    cloud.database().collection('all_user')
      .where({
        _openid: event.openid
      })
      .update({
        data: {
          exe: cloud.database().command.push({
            time: event.time,
            reason: event.reason,
            exeDetail:event.exeDetail
          }),
        }
      })
     return exe
  }
}